package de.zainodis.commons.communication.local;

import java.util.UUID;

import de.zainodis.commons.utils.StringUtils;

/**
 * Base implementation for callbacks. Implements basics for comparing callbacks.
 * Callbacks are identified by a unique id. Each instance has it's own unique
 * id.
 * 
 * @author zainodis
 * 
 */
public abstract class Callback {

   private final String id = UUID.randomUUID().toString();
   private boolean autoUnsubscribe = false;

   public Callback() {
   }

   public Callback(boolean autoUnsubscribe) {
	 this.autoUnsubscribe = autoUnsubscribe;
   }

   @Override
   public String toString() {
	 return id;
   }

   @Override
   public int hashCode() {
	 // ID's are used to compare callback instances
	 return toString().hashCode();
   }

   @Override
   public boolean equals(Object o) {
	 if (o == null) {
	    return false;
	 }
	 return StringUtils.equals(toString(), o.toString());
   }

   public abstract void execute(Object... arguments);

   /**
    * @return true if the callback should automatically be unsubscribed after
    *         {@link Callback#execute(Object...)} has been called; false
    *         otherwise. The default is false.
    */
   public boolean isAutoUnsubscribe() {
	 return autoUnsubscribe;
   }
}
